﻿Imports System.Globalization
Imports System.Configuration
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Reflection

Public Class CUtilities
    'Public Shared Function LINQToDataTable(Of T)(ByVal linqList As IEnumerable(Of T)) As DataTable
    '    Dim dtReturn = New DataTable()
    '    Dim columnNameList As PropertyInfo() = Nothing

    '    If linqList Is Nothing Then
    '        Return dtReturn
    '    End If

    '    For Each t As T In linqList
    '        ' Use reflection to get property names, to create table, Only first time, others will follow 
    '        If columnNameList Is Nothing Then
    '            columnNameList = DirectCast(t.[GetType](), Type).GetProperties()

    '            For Each columnName As PropertyInfo In columnNameList
    '                Dim columnType As Type = columnName.PropertyType

    '                If (columnType.IsGenericType) AndAlso (columnType.GetGenericTypeDefinition() = GetType(Nullable(Of ))) Then
    '                    columnType = columnType.GetGenericArguments()(0)
    '                End If

    '                dtReturn.Columns.Add(New DataColumn(columnName.Name, columnType))
    '            Next
    '        End If

    '        Dim dataRow As DataRow = dtReturn.NewRow()

    '        For Each columnName As PropertyInfo In columnNameList
    '            dataRow(columnName.Name) = If(columnName.GetValue(t, Nothing) Is Nothing, DBNull.Value, columnName.GetValue(t, Nothing))
    '        Next

    '        dtReturn.Rows.Add(dataRow)
    '    Next

    '    Return dtReturn
    'End Function

#Region "DocSoRaChu"
    Public Shared Function docchu(ByVal so1 As Double) As String
        Dim tram As Integer, chuc As Integer, dv As Integer, so As Integer
        Dim chu(0 To 9) As String, chuoi1, chuoi2, chuoi
        so = so1
        chu(0) = "không"
        chu(1) = "một"
        chu(2) = "hai"
        chu(3) = "ba"
        chu(4) = "bốn"
        chu(5) = "năm"
        chu(6) = "sáu"
        chu(7) = "bảy"
        chu(8) = "tám"
        chu(9) = "chín"
        tram = so \ 100
        so = so Mod 100
        chuc = so \ 10
        dv = so Mod 10
        Select Case chuc
            Case 0
                chuoi2 = IIf(tram = 0, "", IIf(dv = 0, "", "lẻ"))
            Case 1
                chuoi2 = "mười"
            Case Else
                chuoi2 = chu(chuc) + " mươi"
        End Select
        Select Case tram
            Case 0
                chuoi1 = ""
            Case Else
                chuoi1 = chu(tram) + " trăm "
        End Select
        Select Case dv
            Case 0
                chuoi = ""
            Case 1
                chuoi = IIf((chuoi2 = "") Or (chuoi2 = "mười") Or (chuoi2 = "lẻ"), "một", "mốt")
            Case 5
                chuoi = IIf((chuoi2 = "lẻ") Or (chuoi2 = ""), "năm", "lăm")
            Case Else
                chuoi = chu(dv)
        End Select

        docchu = Trim(chuoi1 + IIf(chuoi2 <> "", chuoi2, "") + " " + chuoi)
    End Function

    Public Shared Function doc(ByVal so1 As Double) As String
        Dim chuoi1, chuoi2, chuoi3, chuoi4 As String
        chuoi1 = ""
        If so1 = 0 Then
            doc = "không"
            Exit Function
        End If
        Dim so As Double
        so = so1
        Dim ty As Integer, trieu As Integer, ngan As Integer, dv As Integer, rr As Double
        ty = Int(so / 1000000000)
        rr = ty
        so = so - CDbl(1000000000) * CDbl(ty)
        trieu = so \ 1000000
        so = so Mod 1000000
        ngan = so \ 1000
        dv = so Mod 1000
        If ty > 1000 Then
            chuoi1 = doc(rr) + " tỷ"
        ElseIf ty > 0 Then
            chuoi1 = docchu(ty) + " tỷ"
        End If
        chuoi2 = IIf(trieu > 0, docchu(trieu) + " triệu", "")
        chuoi3 = IIf(ngan > 0, docchu(ngan) + " ngàn", "")
        chuoi4 = IIf(dv > 0, docchu(dv), "")
        doc = Trim(chuoi1 + IIf(chuoi2 <> "", " " + chuoi2, "") + IIf(chuoi3 <> "", " " + chuoi3, "") + IIf(chuoi4 <> "", " " + chuoi4, ""))
    End Function

    Public Shared Function NumToStringVietNam(ByVal so As Double)
        Dim so1 As Double, le As String, chuoi As String
        'chuoi = Format(so, "000000000000000000.00")
        so1 = Math.Round(so) 'Val(Left(chuoi, Len(chuoi) - 3))
        le = 0 'Right(chuoi, 2)
        If Val(le) > 0 Then
            If Val(le) < 10 Then
                chuoi = doc(so1) + " phẩy không " + doc(Val(le))
            Else
                chuoi = doc(so1) + " phẩy " + doc(Val(le))
            End If
        Else
            chuoi = doc(so1)
        End If
        If (chuoi.Length <> 0) Then
            Dim c As String
            c = chuoi.Substring(0, 1)
            chuoi = c.ToUpper() + chuoi.Substring(1)
        End If
        NumToStringVietNam = chuoi + " đồng"
    End Function
#End Region
    Public Shared Function FormatMoney(ByVal money As Object) As String
        Dim str As String = money.ToString()
        Dim pattern As String = "(?<a>\d*)(?<b>\d{3})*"
        Dim m As Match = Regex.Match(str, pattern, RegexOptions.RightToLeft)
        Dim sb As StringBuilder = New StringBuilder

        For Each i As Capture In m.Groups("b").Captures
            sb.Insert(0, "," + i.Value)
        Next

        sb.Insert(0, m.Groups("a").Value)
        Return sb.ToString().Trim(",")

    End Function
    Public Shared Sub FormatCurr(ByVal txt As TextBox)
        Dim str As String = txt.Text
        Dim start As Integer = txt.Text.Length - txt.SelectionStart
        str = str.Replace(",", "")
        txt.Text = FormatMoney(str)
        txt.SelectionStart = -start + txt.Text.Length
    End Sub
    Public Shared Sub FormatNumber(ByVal txt As TextBox)
        If (txt.Text <> "") Then
            txt.Text = Convert.ToDecimal(txt.Text).ToString("#,##0.##")
        Else
            txt.Text = "0"
        End If
    End Sub

    Public Shared Function CheckNumberInTextbox(ByVal txt As TextBox) As Boolean
        If (txt.Text <> "") Then
            If (IsNumeric(txt.Text) = False) Then
                MessageBox.Show("Bạn phải nhập số!")
                txt.Text = ""
                txt.Focus()
                Return False
            End If
        Else
            txt.Text = 0
        End If
        Return True
    End Function
    Public Shared Function CheckDateInMaskTextBox(ByVal txt As MaskedTextBox, ByVal dinhDang As String) As Boolean
        If (txt.Text <> "  /  /") Then
            If (CUtilities.CheckDate(txt.Text, dinhDang) = False) Then
                MessageBox.Show("Bạn phải nhập ngày ở định dạng " + dinhDang)
                txt.Focus()
                Return False
            End If
        End If
        Return True
    End Function
    Public Shared Function CheckDate(ByVal s As String, ByVal dinhDang As String) As Boolean
        If (s = "") Then
            Return True
        End If
        Dim kq As Boolean = True
        Try
            Dim culture As CultureInfo
            If (dinhDang.ToLower() = "ngày/tháng/năm") Then
                culture = New CultureInfo("fr-FR", True)
            Else
                culture = New CultureInfo("en-US", True)
            End If
            Dim ngayThangNam As DateTime = DateTime.Parse(s, culture, DateTimeStyles.NoCurrentDateDefault)
        Catch ex As Exception
            kq = False
        End Try
        Return kq
    End Function
    Public Shared Function GetDateFromString(ByVal s As String, ByVal dinhDang As String) As Date
        Dim kq As Date = Nothing
        Try
            Dim culture As CultureInfo
            If (dinhDang.ToLower() = "ngày/tháng/năm") Then
                culture = New CultureInfo("fr-FR", True)
            Else
                culture = New CultureInfo("en-US", True)
            End If
            kq = DateTime.Parse(s, culture, DateTimeStyles.NoCurrentDateDefault)
        Catch ex As Exception
        End Try
        Return kq
    End Function
End Class
